<?php
$vars = parse_ini_file("../ini/settings.ini");
$connect = mysql_connect($vars["mysql_host"],$vars["mysql_username"],$vars["mysql_password"]) or die(mysql_error());
$db = mysql_select_db($vars["mysql_database"],$connect);

$title = $_POST['title'];
$display_name = $_POST['display_name'];
$channel = $_POST['channel'];
$call_sign = $_POST['call_sign'];

$sql = "INSERT into recording_shows (title,display_name,channel,call_sign) values (\"$title\",\"$display_name\",\"$channel\",\"$call_sign\")";
$result = mysql_query($sql,$connect);

//find upcoming shows and add them so we record them
$sql = "select * from listings where title=\"$title\" and previously_shown =\"\" and channel=\"$call_sign\"" ;
$result = mysql_query($sql,$connect) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
        $start = strtotime($row[1]);
        $newstart = strtotime('-1 minute',$start);
        $start = date("Y-m-d H:i:s",$newstart);

        $end = strtotime($row[2]);
        $newend = strtotime('+3 minutes',$end);
        $end = date("Y-m-d H:i:s",$newend);
        $title = $row[4];
	$title_no_change = $title;
        $channel = $channel;
        $episode_num_onscreen = $row[9];
        $sub_title=$row[12];
	$episode_num_dd_progid=$row[8];


        $guess_season=00;
        $guess_episode=00;

        if ($episode_num_onscreen != "") {
		if ($title_no_change == "House Hunters") {
                        $guess_season = substr($episode_num_onscreen,0,2);
                        $guess_episode = substr($episode_num_onscreen,2);
		} else {
                	$guess_season = substr($episode_num_onscreen,0,1);
                	$guess_episode = substr($episode_num_onscreen,1);
		}
        }
        $ep = preg_replace("/[^a-zA-Z0-9\s]/", "", $title);

        if ($sub_title == "" && $title_no_change == "House Hunters" || $sub_title == "" && $title_no_change == "Yard Crashers" || $sub_title == "" && $title_no_change == "House Hunters International") {
                $sub_title = "Episode " . intval($guess_episode);
        }

        if (strlen($guess_episode) == 1) {
                $guess_episode="0".$guess_episode;
        }
	if (strlen($guess_season) >=2) {
        	$ep .= ".S".$guess_season."E".$guess_episode;
	} else {
        	$ep .= ".S0".$guess_season."E".$guess_episode;
	}
	/*
        if ($ep == "S00E00" && $sub_title != "First Look") {
                //get the last one for this show.
                $sql ="select filename from torecord where recording_title=\"$title_no_change\" order by start desc limit 1";
                $r = mysql_query($sql,$connect) or die (mysql_error());
                if (mysql_num_rows($r) == 1) {
                        $tt = preg_replace("/[^a-zA-Z0-9\s]/", "", $title);
                        $ep_n = str_replace(" ",".",$tt);
                        $rr = mysql_fetch_row($r);
                        $last_episode=$rr[0];
                        $last_ep_num = str_replace($ep_n,"",$last_episode);
                        $last_ep_num = explode(".",$last_ep_num);
                        $last_ep_num = $last_ep_num[1];
                        $last_ep_num_ep = explode("E",$last_ep_num);
                        $last_ep_num_se = explode("S",$last_ep_num_ep[0]);
                        $last_ep_num_se = $last_ep_num_se[1];
                        $last_ep_num_ep = $last_ep_num_ep[1];
                        $last_ep_num_ep++;
                        if (strlen($last_ep_num_ep) == 1) {
                                $last_ep_num_ep="0".$last_ep_num_ep;
                        }
                        #$ep = "S".$last_ep_num_se."E".$last_ep_num_ep;
                }
        }
	*/

	$sub_title = str_replace("&","and",$sub_title);
        $sub_title = preg_replace("/[^a-zA-Z0-9\s]/", "", $sub_title);
        $ep .= ".".$sub_title;
        $ep = str_replace(" ",".",$ep);
        //echo $ep ." $start $end\n";
	$sql = "SELECT count(*) from torecord where episode_num_dd_progid=\"$episode_num_dd_progid\" and recording_title =\"$title_no_change\"";
	$result3 = mysql_query($sql,$connect) or die (mysql_error());
	$row2 = mysql_fetch_row($result3);
        if ($sub_title == "") {
        	$sub_title = "Waiting...";
        }
	if ($row2[0] == 0) {
        	$sql = "INSERT into torecord (start,end,filename,channel,recording_title,recording_subtitle,episode_num_dd_progid) values (\"$start\",\"$end\",\"$ep\",\"$channel\",\"$title\",\"$sub_title\",\"$episode_num_dd_progid\")";
		$result2 = mysql_query($sql,$connect);
	} else {
                $sql = "SELECT manual from torecord where episode_num_dd_progid=\"$episode_num_dd_progid\" and recording_title=\"$title\"";
                $result2 = mysql_query($sql,$connect);
                $row7=mysql_fetch_row($result2);
                $manual = $row7[0];
                if ($manual == 1) {
                        $sql = "UPDATE torecord set start=\"$start\",end=\"$end\",recording_subtitle=\"$sub_title\" where episode_num_dd_progid=\"$episode_num_dd_progid\" and recording_title=\"$title\"";
                        $result2 = mysql_query($sql,$connect);
		} else {
                	$sql = "UPDATE torecord set start=\"$start\",end=\"$end\",recording_subtitle=\"$sub_title\",filename=\"$ep\" where episode_num_dd_progid=\"$episode_num_dd_progid\" and recording_title=\"$title\"";
                	$result2 = mysql_query($sql,$connect);
		}
        }

}
echo "Recording $title";
?>
